﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SmartMD.Manager.Common;
using SmartMD.DataModel.Entity;
using System.Collections.ObjectModel;
using SmartMD.DataAccess.Service;
using Techstar.Dialog;

namespace SmartMD.Manager.Patient
{
    /// <summary>
    /// Interaction logic for StoreInfoPanel.xaml
    /// </summary>
    public partial class StoreInfoPanel : UserControl
    {
        public StoreInfoPanel()
        {
            InitializeComponent();
            
            this.ucToolBar.saveHandler = Save;
            this.ucToolBar.newHandler = New;
            this.ucToolBar.deleteHandler = Delete;
            this.ucToolBar.editHandler = Edit;
            this.ucPatientInfoEdit.ower = this;
            this.ucPatientList.Owner = this;
        }
        //新建对话框
        private NewWindow dlg = null;
        private void New()
        {
            if (this.ucPatientList.patientInfos != null && this.ucPatientList.patientInfos.Count > 0)
            {
                int itemNum = this.ucPatientList.patientInfos.Count;
                PatientInfo pInfo = this.ucPatientList.patientInfos[itemNum - 1];
                if (!Validate("住院号", pInfo.HospitalNo))
                {
                    TaskDialogEx.Warning("提示信息",  "住院号重复，请重新输入！");
                    return;
                }
                if (!Validate("门诊号", pInfo.ClinicNo))
                {
                    TaskDialogEx.Warning("提示信息", "门诊号重复，请重新输入！");
                    return;
                }
                if (!Validate("随访号", pInfo.FollowUpNo))
                {
                    TaskDialogEx.Warning("提示信息", "随访号重复，请重新输入！");
                    return;
                }
            }
            dlg = new NewWindow();
            dlg.Title = "新建患者信息";
            dlg.label1.Content = "姓   名";
            dlg.label2.Content = "描   述";
            dlg.okHandler = Dlg_OK_Click;
            dlg.ShowDialog();           
        }

        private bool Validate(string key, string value)
        {
            if (value == null)
                return true;
            bool success = true;
            PatientInfo tmpInfo = null;
            for (int j = 0; j < this.ucPatientList.patientInfos.Count - 1; j++)
            {
                string no = null;
                if (key == "门诊号")
                {
                    no = this.ucPatientList.patientInfos[j].ClinicNo;
                }
                else if (key == "住院号")
                {
                    no = this.ucPatientList.patientInfos[j].HospitalNo;
                }
                else if (key == "随访号")
                {
                    no = this.ucPatientList.patientInfos[j].FollowUpNo;
                }
                if (value == no)
                {        
                    return false;
                }
            }
            tmpInfo = DataServiceManager.PatientInfoService.GetPatientInfo(key, value);
            if (tmpInfo != null)
            {
                success = false;
            }
            return success;
        }

        private void Dlg_OK_Click(object sender, RoutedEventArgs e)
        {
            PatientInfo patientInfo = new PatientInfo();
            patientInfo.Id = System.Guid.NewGuid().ToString();
            patientInfo.State = DataModel.RecordState.Added;
            patientInfo.Birthday = System.DateTime.Now;
            patientInfo.FirstFollowUpTime = System.DateTime.Now;
            if(dlg != null)
            {
                patientInfo.Name = dlg.txt1.Text;
                dlg.Close();
            }
            this.ucPatientInfoEdit.IsEnabled = true;
            this.ucPatientInfoEdit.DataSource = patientInfo;
            if (this.ucPatientList.patientInfos == null)
            {
                this.ucPatientList.patientInfos = new ObservableCollection<PatientInfo>();
            }
            this.ucPatientList.patientInfos.Add(patientInfo);
        }

        private void Save()
        {
            bool success = true;
            if (this.ucPatientList.patientInfos == null || this.ucPatientList.patientInfos.Count <= 0)
                return;
            ObservableCollection<PatientInfo> patientInfoList =  this.ucPatientList.patientInfos;
            ObservableCollection<PatientInfo> tmpPatientInfoList = new ObservableCollection<PatientInfo>();
            PatientInfo tmpPatientInfo;
            try
            {
                for (int j = 0; j < this.ucPatientList.patientInfos.Count; j++)
                {
                    tmpPatientInfo = this.ucPatientList.patientInfos[j];
                    if (tmpPatientInfo.State == DataModel.RecordState.Added || tmpPatientInfo.State == DataModel.RecordState.Modified)
                    {
                        tmpPatientInfoList.Add(tmpPatientInfo);
                    } 
                    if(tmpPatientInfo.State == DataModel.RecordState.Deleted)
                    {
                       bool deleted = DataServiceManager.PatientInfoService.DeleteObject(tmpPatientInfo);
                       if (deleted)
                       {
                           //在控件中删除对象
                           this.ucPatientList.patientInfos.Remove(tmpPatientInfo);
                       }
                    }
                }
                success = DataServiceManager.PatientInfoService.savePatientInfos(tmpPatientInfoList);
                
               if (success)
               {
                   TaskDialogEx.Information("保存成功！");
                   for (int j = 0; j < tmpPatientInfoList.Count; j++)
                   {
                       tmpPatientInfoList[j].State = DataModel.RecordState.Normal;
                   }
                   
                   this.ucPatientInfoEdit.IsEnabled = true;
                   this.ucPatientList.patientInfos = null;
                   this.ucPatientInfoEdit.DataSource = null;
               }
            }
            catch (Exception ex)
            {
                TaskDialogEx.Information("保存失败", "失败原因：" + ex.Message);
                Logger.logger.Error(ex.Message);
            }
        }

        private void Delete()
        {
            this.ucPatientList.SetDeleteFlag();
        }

        internal void OnWindowActivated()
        {
            this.ucPatientInfoEdit.InitAllComboxValue();
        }

        private void Edit()
        {
            this.ucPatientInfoEdit.IsEnabled = true;
        }
    }
}
